class Admin::GroupsController < Admin::AdminApplicationController
  layout "admin"
  before_filter :admin_login_required
  
  def index
    @groups = Group.paginate :page => params[:groups], :order => "created_at desc"
  end
  
  def search
    if params[:search_type].nil?
      redirect_back_or_default admin_groups_url
    elsif params[:search_type] == 'id'
      @group = Group.find(:first, :conditions => ["id = ?", params[:q]])
    elsif params[:search_type] == 'content'
      #@groups = Group.find_by_contents(params[:q])
      @groups = Group.paginate_search(params[:q], {:page => params[:page]})
    else
      redirect_back_or_default admin_groups_url
    end
  end
  
  def show
    @group = Group.find(params[:id])
    @users = User.paginate :conditions => ["gu.group_id = ?", @group.id], :joins => "as us inner join group_users as gu on us.id = gu.user_id", :select => "us.*", :page => params[:page]
  end
  
  def edit
    @group = Group.find(params[:id])
  end
  
  def update
    @group = Group.find(params[:id])
    @group.update_attributes(params[:group])
    redirect_to admin_group_url(@group)
  end
  
  def destroy
    @group = Group.find(params[:id])
    
    @group.destroy
    redirect_to admin_groups_url
  end
  
  def kick_user
    @group = Group.find(params[:id])
    @user = User.find(params[:user])
    gu = GroupUser.find_by_group_id_and_user_id(@group, @user)
    gu.destroy unless gu.is_admin == 't'
    redirect_to admin_group_url(@group)
  end
end
